import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import MathUtil from '@/MathUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import MovementCommon from 'code/Core/Common/MovementCommon';
import SelectCommon from 'code/Core/Common/SelectCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 火球术
 **/
export default class FS_FireBall extends AbilityBase {
    static instance: FS_FireBall;
    buffType: AppBuffType = {
        id: 'buff_FS_FireBall',
        art: this.art,
        name: this.abilityName,
        ubertip: '持续受到火焰伤害',
        dur: 20,
        targetArt: 'Abilities\\Spells\\Other\\SoulBurn\\SoulBurnbuff.mdl',
        targetAttach: 'overhead',
        onInterval: this.buffInterval.bind(this),
        interval: 2,
        stackCountMax: -1,
        isDebuff: true,
    };
    constructor(id: string) {
        super(id);
        FS_FireBall.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let ag = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), tarX, tarY);
        MovementCommon.LineMissile({
            missile: 'Abilities\\Weapons\\RedDragonBreath\\RedDragonMissile.mdl',
            angle: ag,
            distance: 800,
            time: 1,
            caster: source,
            radius: 60,
            onHit: this.onMissileHit.bind(this),
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    onMissileHit(p: LineMissileConfig, hit: unit) {
        let lv = this.getAbilityLevel(p.caster);
        let dmg = 120 * lv + UnitCommon.取法强(p.caster) * (0.6 + 0.4 * lv);
        DamageSystemInstance.applyFireDamage(hit, p.caster, dmg, this.id);
        // let buff = BuffUtil.addBuff(hit, this.buffType.id, p.caster);
        let buff = BuffCommon.addBuff({ target: hit, caster: p.caster, buffTypeId: this.buffType.id, ablityId: this.id });
        if (buff.stackCount >= 6) {
            let es = SelectCommon.getAllyUnitInRangeExcludeCenterUnit(hit, 275);
            for (let e of es) {
                MovementCommon.TargetMissile({
                    missile: 'Abilities\\Spells\\Other\\BreathOfFire\\BreathOfFireDamage.mdl',
                    target: e,
                    caster: p.caster,
                    speed: 600,
                    radian: 0.35,
                    curX: GetUnitX(hit),
                    curY: GetUnitY(hit),
                    onReach: (p) => {
                        let lv = this.getAbilityLevel(p.caster);
                        let dmg = 160 * lv + UnitCommon.取法强(p.caster) * (0.6 + 0.4 * lv);
                        DamageSystemInstance.applyFireDamage(p.target, p.caster, dmg, this.id);
                    },
                });
            }
        }
    }
    buffInterval(buff: Buff) {
        let lv = this.getAbilityLevel(buff.getCaster());
        let dmg = 160 * lv + UnitCommon.取法强(buff.getCaster()) * (0.6 + 0.4 * lv);
        DamageSystemInstance.applyFireDamage(buff.getParent(), buff.getCaster(), dmg * buff.stackCount * 0.05, this.id, true);
    }
}
